package com.chenzhou.qx.library;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * 设备号获取工具
 */
public class DeviceIdUtil {

    private static final String TAG = DeviceIdUtil.class.getName();

    public static void init(Context context) {
        init(context, false);
    }

    public static void init(Context context, boolean debug) {
        MsaIdHelper.getInstance(context).setDebug(debug).init();
    }

    /**
     * 获取唯一标识
     * <p>
     * 优先级：imei > oaid > android_id > 0
     *
     */
    public static String getId(Context context) {
        //imei
        String id = getImei(context);

        //oaid
        if (TextUtils.isEmpty(id)) {
            id = getOaid(context);
        }

        //android_id
        if (TextUtils.isEmpty(id)) {
            id = getAndroidId(context);
        }

        if (TextUtils.isEmpty(id)) {
            id = "0";
        }

        return id;
    }

    /**
     * 获取唯一标识
     * <p>
     * 优先级：oaid > android_id > 空字符
     *
     */
    public static String getIdNew(Context context) {
        return getIdNew(context,"");
    }

    public static String getIdNew(Context context, String defaultValue) {
        String id = getOaid(context);
        if (TextUtils.isEmpty(id)) {
            id = getAndroidId(context);
        }
        if (TextUtils.isEmpty(id)) {
            id = defaultValue;
        }
        return id;
    }

    public static String getOaid(Context context) {
        String id = "";
        if (MsaIdHelper.getInstance(context).isSupportOaid()) {
            id = MsaIdHelper.getInstance(context).getOaid();
        }
        return id;
    }

    public static String getAaid(Context context) {
        String id = "";
        if (MsaIdHelper.getInstance(context).isSupportOaid()) {
            id = MsaIdHelper.getInstance(context).getAaid();
        }
        return id;
    }

    public static String getVaid(Context context) {
        String id = "";
        if (MsaIdHelper.getInstance(context).isSupportOaid()) {
            id = MsaIdHelper.getInstance(context).getVaid();
        }
        return id;
    }

    /**
     * 获取IMEI
     *
     * @param context
     * @return
     */
    @SuppressLint({"MissingPermission", "HardwareIds"})
    public static String getImei(Context context) {
        try {
            TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if(tel!= null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    return tel.getImei();
                } else {
                    return tel.getDeviceId();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取AndroidID
     */
    public static String getAndroidId(Context context) {
        try {
            @SuppressLint("HardwareIds")
            String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            return androidId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
